package ihistoryApp.dal.controller;
import ihistoryApp.request.RankRequest;
import ihistoryApp.request.RegisterRequest;
import ihistoryApp.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/game")
public class RankController {
    @Autowired
    private RankService rankService;
    @PostMapping("/rank")
    public ResponseEntity<Map<String,Object>> getRank(@RequestBody RankRequest request) {
        System.out.println("sssssssssssssssssssssssssssssdasdsdsadasd");
        rankService.updateScoreAndNotify(request.getUid(),request.getNickName(),request.getLevel());
        Map<String, Object> rank = getTopRanks();
        return ResponseEntity.ok(rank);
    }

    private Map<String, Object> getTopRanks() {
        // 从Redis获取排行榜数据
        Set<ZSetOperations.TypedTuple<String>> ranks = rankService.getTopLevel();
        Map<String, Object> rank = new LinkedHashMap<>();
        rank.put("ranks", ranks);
        return rank;
    }
}
